PRO MAKE_DIST_COMPARISON_PLOTS
  
  ;;============================================================================
  ;; Training Set comparison plots... LEFT = IRDC MODEL, 
  ;;  RIGHT = MORPH MATCH
  
  COMMON FFORE_BLOCK,n,rb3,R0,d,R,Z,tau,f_data,sig_f,corr,farlist,do_Tfit,$
     lpstr,rho_hi,rho_h2,rho_star
  
  IF n_elements(rb3) EQ 0 THEN restore,'./irdc_dist_model/bgps_rb3.sav',/ver
  n = n_elements(rb3)
  angsep = 4.
  range = [0,16]
  
  masercolor = 'Magenta'
  nirexcolor = 'Cyan'
  fullrb3 = rb3
  
  ;; Clear out rb3 sources NOT well constrained
  cind = where(rb3.constrain EQ 1, nc)
  print,string(nc,n_elements(rb3),format="('N constrained: ',I0,' of ',I0)")
  grb3 = rb3[cind]
  
  ;; Just select sources associated with GRS objects
  pind = where(grb3.grs EQ 1, np)
  print,string(np,angsep,format=$
               "('GRS-associated objects: ',I0,' at angsep = ',I0,' arcmin')")
  grb3 = grb3[pind]
  
  
  ;;==========================================================================
  ;; Set up the plotting environment
  
  myps,'./irdc_dist_model/analysis_plots/distance_comparison_paper.eps',$
       xsize=8.5,/cmyk
  multiplot,[2,1]               ;,xgap=0.035
  
  ;; Set up plot window
  plot,[0],[0],xr=range,/xst,yr=range,/yst,/isotropic,$
       xtit='IRDC Modeling Distance [kpc]',ytit='Comparison Set Distance [kpc]'
  
  ;; Plot the 'alley'
  plots,0,1
  plots,range[1]-1,range[1],/cont,linestyle=2,color=cgColor('BLK5')
  plots,1,0
  plots,range[1],range[1]-1,/cont,linestyle=2,color=cgColor('BLK5')
  
  
  
  ;;============================================================================
  ;; Plot masers 
  restore,'./irdc_dist_model/training_set.sav',/ver
  restore,'./irdc_dist_model/data/case2_irdc_TdFIT.sav',/ver
  nt = n_elements(training)
  trb3 = replicate( create_struct( 'cnum',0L,'kdn',0.,'kdf',0.,'d',0.d,$
                                   'morphd',0.,'td',0.,'tdm',0.,'tdp',0.,$
                                   'use',0b,'mdm',0.,'mdp',0.), n)
  FOR i=0L, n-1 DO BEGIN
     
     IF (i+1) MOD 100 EQ 0 THEN $
        print,'Maser: Processing object '+string(i+1,format="(I3)")+$
              ' of '+string(n,format="(I0)")
     trb3[i].d      = rb3[i].fitdist
     trb3[i].morphd = rb3[i].morph_d
     trb3[i].mdm    = rb3[i].morph_sigm
     trb3[i].mdp    = rb3[i].morph_sigp
     
     
  ENDFOR
  trb3.cnum = rb3.cnum
  
  FOR i=0L, nt-1 DO BEGIN
     ;; Select by distance
     gcirc,2,training[i].l,training[i].b,rb3.l,rb3.b,dis
     delv = abs(rb3.vlsr-training[i].vlsr)
     
     ind = WHERE(dis LE 0.25*3600. AND delv LE 10., nrt)
     print,training[i].name, nrt
     
     IF nrt NE 0 THEN BEGIN
        trb3[ind].use = 1b
        trb3[ind].td  = training[i].d
        trb3[ind].tdm = training[i].dm
        trb3[ind].tdp = training[i].dp
     ENDIF
  ENDFOR
  
  ind = WHERE(trb3.use NE 0, nuse)
  print,nuse
  trb3 = trb3[ind]
  print,nearfar[ind]
  print,trb3.cnum
  
  ;; Maser error bars
  oploterror,trb3.d,trb3.td,trb3.td*0.,trb3.tdm,/lobar,$
             psym=3,color=cgColor('Dark Green'),errcolor=cgColor('BLK4')
  oploterror,trb3.d,trb3.td,trb3.td*0.,trb3.tdp,/hibar,$
             psym=3,color=cgColor('Dark Green'),errcolor=cgColor('BLK4')
  
  ;;=======================================
  ;; Plot GRS 
  oplot,grb3.fitdist,grb3.grs_dist,psym=8
  junk = WHERE(abs(grb3.fitdist-grb3.grs_dist)GT 1.,nbad)
  
  
  
  
  ;;=======================================
  ;; PLOT MARSHALL ET AL. (2009) NIREX DISTANCES
  m09 = read_mrt('./local/mrt/Marshall2009_Table1.txt')
  m09ind = lonarr(n)
  m09dist = fltarr(n)
  FOR i=0L, n-1 DO BEGIN
     gcirc,2,rb3[i].l,rb3[i].b,m09.glon,m09.glat,dis
     d_min = min(dis,mind)
     m09ind[i] = mind
     
     IF d_min LE 1.*60. THEN BEGIN
        print,d_min, rb3[i].morph_d,m09[mind].dist
        m09dist[i] = m09[mind].dist
     ENDIF
  ENDFOR
  
  mmind = where(m09dist NE 0., nmm) & print,nmm
  
  oplot,rb3[mmind].fitdist,m09dist[mmind],psym=cgSymcat(14),$
        color=cgColor(nirexcolor),symsize=0.75
  m09bad = where( abs(rb3[mmind].fitdist-m09dist[mmind]) GT 1., nmbad)
  
  
  
  ;; Maser points, themselves -- to be in front
  oplot,trb3.d,trb3.td,psym=cgSymcat(17),color=cgColor(masercolor),$
        symsize=0.75
  
  
  
  al_legend,['GRS','Maser','NIREX'],box=0,/top,/left,$
            psym=[16,17,14],symsize=[0.5,0.75,0.75],charsize=0.75,$
            color=['Opposite',masercolor,nirexcolor]
  al_legend,box=0,/bottom,/right,charsize=0.6,$
            ['GRS f!dmatch!n = '+string(float(np-nbad)/float(np),$
                                        format="(F0.2)"),$
             'NIREX f!dmatch!n = '+string(float(nmm-nmbad)/float(nmm),$
                                        format="(F0.2)")]
  
  
  ;;============================================================================
  ;; Comparison with Morphological Matching...
  multiplot,/doxaxis 
  
  ;; Just select sources associated with GRS objects -- refresh
  pind = where(fullrb3.grs EQ 1, np)
  print,string(np,angsep,format=$
               "('GRS-associated objects: ',I0,' at angsep = ',I0,' arcmin')")
  grb3 = fullrb3[pind]


  ;; Set up plot window
  plot,[0],[0],xr=range,/xst,yr=range,/yst,/isotropic,$
       xtit='Morphological Matching Distance [kpc]' ;; ,$
  ;; xtit='Comparison Set Distance [kpc]'
  
  ;; Plot the 'alley'
  plots,0,1
  plots,range[1]-1,range[1],/cont,linestyle=2,color=cgColor('BLK5')
  plots,1,0
  plots,range[1],range[1]-1,/cont,linestyle=2,color=cgColor('BLK5')
  
  ;;=================================================
  ;; All the action goes in here...
  
  ;; Maser error bars
  oploterror,trb3.morphd,trb3.td,trb3.td*0.,trb3.tdm,/lobar,$
             psym=3,errcolor=cgColor('BLK4')
  oploterror,trb3.morphd,trb3.td,trb3.td*0.,trb3.tdp,/hibar,$
             psym=3,errcolor=cgColor('BLK4')
  
  ;;Morphological Match error bars
  oploterror,trb3.morphd,trb3.td,trb3.mdm,trb3.tdm*0.,/lobar,$
             psym=3,errcolor=cgColor('BLK4')
  oploterror,trb3.morphd,trb3.td,trb3.mdp,trb3.tdp*0.,/hibar,$
             psym=3,errcolor=cgColor('BLK4')
  
  ;;=======================================
  ;; Plot GRS
  plotsym,0,0.5,/fill
  oplot,grb3.morph_d,grb3.grs_dist,psym=8,color=cgColor('Opposite')
  junk = WHERE(abs(grb3.morph_d-grb3.grs_dist)GT 1.,nbad)
  
  print,m4_stat(grb3.grs_dist),m4_stat(grb3.morph_d)
  
  ;;=======================================
  ;; PLOT MARSHALL ET AL. (2009) NIREX DISTANCES
  oplot,rb3[mmind].morph_d,m09dist[mmind],psym=cgSymcat(14),$
        color=cgColor(nirexcolor),symsize=0.75
  m09bad = where( abs(rb3[mmind].morph_d-m09dist[mmind]) GT 1., nmbad)
  
   ;; Maser points, themselves -- to be in front
  oplot,trb3.morphd,trb3.td,psym=cgSymcat(17),color=cgColor(masercolor),$
        symsize=0.75
  
  
  al_legend,['GRS','Maser','NIREX'],box=0,/top,/left,$
            psym=[16,17,14],symsize=[0.5,0.75,0.75],charsize=0.75,$
            color=['Opposite',masercolor,nirexcolor]
  al_legend,box=0,/bottom,/right,charsize=0.6,$
            ['GRS f!dmatch!n = '+string(float(np-nbad)/float(np),$
                                        format="(F0.2)"),$
             'NIREX f!dmatch!n = '+string(float(nmm-nmbad)/float(nmm),$
                                        format="(F0.2)")]
  
  myps,/done,/mp
  
END
